﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NBusiness.CodeDom.Services;
using NBusiness.CodeDom;
using csla = Csla;

namespace NBusiness.Frameworks.Csla
{
    public class CslaFramework : IFramework
    {
        IReflectionService _reflectionService;

        public CslaFramework(IReflectionService reflectionService)
        {
            _reflectionService = reflectionService;
        }

        #region IFrameworkService Members

        public EntityValidation[] GetValidationRules()
        {
            Type ruleType = typeof(csla.Validation.RuleHandler);
            MethodData invoke = new MethodData(ruleType.GetMethod("Invoke"));

            return (from t in _reflectionService.FindCallableAs(invoke)
                    select new CslaValidationRule(
                        t.FullName,
                        (from key in t.Signature.Keys
                         select new CslaValidationArgument
                         {
                             Name = key,
                             Type = t.Signature[key]
                         }).ToArray()))
                         .ToArray();
        }

        public EntityAccess[] GetAccessRules()
        {
            return new EntityAccess[] { new CslaAccessRule { Name = "csla.validation.commonrules.role" } };
        }

        public EntityAuthorization[] GetAuthorizationRules()
        {
            return new EntityAuthorization[] { new CslaAuthorizationRule { Name = "csla.validation.commonrules.role" } };
        }

        #endregion
    }
}
